﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Bonit_It.ViewModel.Cashier
{
    using System.Diagnostics;
    using System.IO;
    using System.IO.Ports;

    using Bon.It.Proxy;

    using Bon_It.Model;

    /// <summary>
    /// A class for creating receipts.
    /// </summary>
    public class ReceiptEngine : IDisposable
    {
        /// <summary>
        /// The output device.
        /// </summary>
        public enum OutputDevices
        {
            /// <summary>
            /// Standard printer
            /// </summary>
            ReceiptPrinter = 2,

            /// <summary>
            /// A receipt printer
            /// </summary>
            WindowsPrinter = 4, 

            /// <summary>
            /// The screen.
            /// </summary>
            Srcreen = 8
        }

        /// <summary>
        /// Gets or sets the port.
        /// </summary>
        /// <value>
        /// The port.
        /// </value>
        private System.IO.Ports.SerialPort Port { get; set; }


        /// <summary>
        /// Gets or sets the beleg.
        /// </summary>
        /// <value>
        /// The beleg.
        /// </value>
        private Belege Beleg { get; set; }


        /// <summary>
        /// Gets the outputevice.
        /// </summary>
        public OutputDevices Outputevice { get; private set; }

        /// <summary>
        /// Inits the specified receipt ID.
        /// </summary>
        /// <param name="receiptID">The receipt ID.</param>
        public void Init(long receiptID, OutputDevices device)
        {
            Outputevice = device;
            Beleg = ServiceProxy.Instance.GetBelegById(receiptID);

        }

        /// <summary>
        /// Initializes the port.
        /// </summary>
        private void InitializePort()
        {
            var ports = SerialPort.GetPortNames();
            if (ports.Count() > 0)
            {
                Port = new SerialPort(ports[0], 9600, Parity.Even, 8);
                if (Port.IsOpen)
                {
                    Port.Close();
                }
                Port.Open();

            }
            else
            {
                throw new IOException("Es ist kein Serieller Anschluss installiert, um den Bon-Drucker anzusprechen!");
            }
        }

        /// <summary>
        /// Deinitializes the port.
        /// </summary>
        private void DeinitializePort()
        {
            if (Port != null)
            {
                Port.Close();
            }
        }
        /// <summary>
        /// Initializes the Bonrol Printer.
        /// </summary>
        private void InitializeBonRollPrinter()
        {
            // initialize the width

            // initialize the printer
            
        }

        /// <summary>
        /// Prints this instance.
        /// </summary>
        public void Print()
        {
            this.InitializePort();
            this.PrintHead();
            this.PrintReceipt();
            this.PrintFooter();
            this.FinishPrinting();
            this.DeinitializePort();
        
        }

        #region Implementation of IDisposable

        /// <summary>
        /// Führt anwendungsspezifische Aufgaben durch, die mit der Freigabe, der Zurückgabe oder dem Zurücksetzen von nicht verwalteten Ressourcen zusammenhängen.
        /// </summary>
        public void Dispose()
        {
            Beleg = null;
            this.DeinitializePort();
        }

        #endregion

        #region private methods


        /// <summary>
        /// Prints the string.
        /// </summary>
        /// <param name="lineToPrint">The line to print.</param>
        private void PrintString(string lineToPrint)
        {
            Port.Write(lineToPrint);
        }

        /// <summary>
        /// Prints the head.
        /// </summary>
        private void PrintHead()
        {
            
        }

        /// <summary>
        /// Prints the receipt.
        /// </summary>
        private void PrintReceipt()
        {
            
        }

        /// <summary>
        /// Prints the footer.
        /// </summary>
        private void PrintFooter()
        {
            
        }

        /// <summary>
        /// Finishes the printing.
        /// </summary>
        private void FinishPrinting()
        {
                
        }


        #endregion
    }
}
